﻿using BDShop.Business;
using BDShop.Business.Cache;
using BDShop.Entities;
using BDShop.Models;
using BDShop.Resources;
using BDShop.Utilities.Managers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using BDShop.Configuration;

namespace BDShop.Controllers
{
    public class BrandController : Controller
    {//
        // GET: /Tag/

        public ActionResult Index(int brandid)
        {
            Brand brand = BrandCache.GetByID(brandid);
            BrandModels brandModel = new BrandModels();
            brandModel.ID = brand.ID;
            brandModel.Name = brand.Name;

            return View(brandModel);
        }

        public ActionResult ProductList(int brandID, string brandName, int sortOrder = 0, int? page = 1, string price = "All", string currentFilter = "")
        {
            int pageSize = BDShopConfiguration.Web.pageSize;

            ViewBag.Url = UrlManager.ProductOfBrandUrl(brandID, brandName);
            //Get value for sort ddl
            List<SelectListItem> lstSort = new List<SelectListItem>();
            SelectListItem itemDefault = new SelectListItem() { Text = FO.macdinh, Value = "0", Selected = (0 == sortOrder) ? true : false };
            SelectListItem itemNameAZ = new SelectListItem() { Text = FO.tensanphamaz, Value = "1", Selected = (1 == sortOrder) ? true : false };
            SelectListItem itemNameZA = new SelectListItem() { Text = FO.tensanphamza, Value = "2", Selected = (2 == sortOrder) ? true : false };
            SelectListItem itemPriceTC = new SelectListItem() { Text = FO.giathapcao, Value = "3", Selected = (3 == sortOrder) ? true : false };
            SelectListItem itemPriceCT = new SelectListItem() { Text = FO.giacaothap, Value = "4", Selected = (4 == sortOrder) ? true : false };
            SelectListItem itemRateH = new SelectListItem() { Text = FO.binhchoncaonhat, Value = "5", Selected = (5 == sortOrder) ? true : false };
            SelectListItem itemRateL = new SelectListItem() { Text = FO.binhchonthapnhat, Value = "6", Selected = (6 == sortOrder) ? true : false };
            IEnumerable<SelectListItem> eItem = new SelectListItem[] { itemDefault, itemNameAZ, itemNameZA, itemPriceTC, itemPriceCT, itemRateH, itemRateL };
            lstSort.AddRange(eItem);
            ViewBag.Sort = lstSort;

            ViewBag.page = page;
            ViewBag.sortOrder = sortOrder;
            ViewBag.Price = price;

            List<ProductCacheComplex> lstProduct = new List<ProductCacheComplex>();
            //Get Product Of Supplier
            lstProduct = ProductCache.Instance.Get().Where(p=>p.BrandID==brandID).ToList();

            switch (sortOrder)
            {
                case 1:
                    lstProduct = lstProduct.OrderBy(p => p.Name).ToList();
                    break;
                case 2:
                    lstProduct = lstProduct.OrderByDescending(p => p.Name).ToList();
                    break;
                case 3:
                    lstProduct = lstProduct.OrderBy(p => p.Price).ToList();
                    break;
                case 4:
                    lstProduct = lstProduct.OrderByDescending(p => p.Price).ToList();
                    break;
                case 5:
                    lstProduct = lstProduct.OrderByDescending(p => Math.Ceiling((float)p.Rating / p.Rater)).ToList();
                    break;
                case 6:
                    lstProduct = lstProduct.OrderBy(p => Math.Ceiling((float)p.Rating / p.Rater)).ToList();
                    break;
                default:
                    lstProduct = lstProduct.OrderByDescending(p => p.DateCreated).ToList();
                    break;
            }

            //Filter by price
            List<ProductCacheComplex> lstProductResult = new List<ProductCacheComplex>();
            if (price == "All")
            {
                lstProductResult = lstProduct;
            }
            else
            {
                string[] lstPrice = price.Split('-');
                foreach (var s in lstPrice)
                {
                    switch (s)
                    {
                        case "0":
                            lstProductResult = lstProductResult.Union(lstProduct.Where(p => p.Price < 100000)).ToList();
                            break;
                        case "1":
                            lstProductResult = lstProductResult.Union(lstProduct.Where(p => p.Price > 100000 && p.Price <= 500000)).ToList();
                            break;
                        case "2":
                            lstProductResult = lstProductResult.Union(lstProduct.Where(p => p.Price > 500000 && p.Price <= 1000000)).ToList();
                            break;
                        case "3":
                            lstProductResult = lstProductResult.Union(lstProduct.Where(p => p.Price > 1000000)).ToList();
                            break;
                    }
                }
            }

            //Paging
            int pageNumber = (page ?? 1);

            return PartialView("Partial/_ProductList", lstProductResult.ToPagedList(pageNumber, pageSize));
        }

        [ChildActionOnly]
        public ActionResult HotBrandLink()
        {
            //Get brand from cache
            List<Brand> lstBrand = BrandCache.Instance.Get();

            if (lstBrand == null)
            {
                lstBrand = new List<Brand>();
            }

            return PartialView("Partial/_HotBrandLink", lstBrand);
        }
    }
}
